﻿namespace DslEditorPowerToy.Languages
{
    using Microsoft.VisualStudio.Modeling;
    using Microsoft.VisualStudio.Modeling.Diagrams;
    using Microsoft.VisualStudio.Modeling.DslDefinition;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;

    public static class ModelHelper
    {
        public static string FormatDslInfo(string input, Dsl dsl)
        {
            string str = input;
            Regex regex = null;
            str = new Regex(@"\{DslName\}", RegexOptions.IgnoreCase).Replace(str, dsl.Name);
            regex = new Regex(@"\{DslDisplayName\}", RegexOptions.IgnoreCase);
            return regex.Replace(str, dsl.DisplayName);
        }

        public static ModelElement GetRepresentedElement(object shapeOrElement)
        {
            ModelElement element = null;
            if (shapeOrElement is ElementListCompartment)
            {
                return ((ElementListCompartment) shapeOrElement).ParentShape.Subject;
            }
            if (shapeOrElement is PresentationElement)
            {
                return ((PresentationElement) shapeOrElement).Subject;
            }
            if (shapeOrElement is ModelElement)
            {
                element = (ModelElement) shapeOrElement;
            }
            return element;
        }

        public static IList<ModelElement> GetRepresentedElements(ICollection shapesOrElements)
        {
            List<ModelElement> list = new List<ModelElement>();
            foreach (object obj2 in shapesOrElements)
            {
                ModelElement representedElement = GetRepresentedElement(obj2);
                if (representedElement != null)
                {
                    list.Add(representedElement);
                }
            }
            return list;
        }
    }
}

